<?php


namespace Tbf\Bundle\ImageBundle;


class ImageUtil {
    /**
     * 检测某个字符旋转后是否超出边界
     * input struct{
     *   pos_x float
     *   pos_y float
     *   height float
     *   width float
     *   rel_x float
     *   rel_y float
     *   max_x float
     *   max_y float
     *   hex float
     * }
     * output bool
     */
    static public function checkFontRotateIsOut($input){
        $rotation_arr = array(
            'base_x'=>$input['pos_x'],
            'base_y'=>$input['pos_y'],
            'hex'=>$input['hex'],
        );
        $rect_arr = array(
            'x_min'=>0,
            'y_min'=>0,
            'x_max'=>$input['max_x'],
            'y_max'=>$input['max_y'],
        );
        $rotation_arr['rel_x'] = -$input['rel_x'];
        $rotation_arr['rel_y'] = -$input['rel_y'];
        $output = self::rotationPoint($rotation_arr);
        if (!self::checkPointInsideRectangle(array_merge($rect_arr,$output))){
            return false;
        }
        $rotation_arr['rel_x'] = $input['width']-$input['rel_x'];
        $rotation_arr['rel_y'] = -$input['rel_y'];
        $output = self::rotationPoint($rotation_arr);
        if (!self::checkPointInsideRectangle(array_merge($rect_arr,$output))){
            return false;
        }
        $rotation_arr['rel_x'] = -$input['rel_x'];
        $rotation_arr['rel_y'] = $input['height']-$input['rel_y'];
        $output = self::rotationPoint($rotation_arr);
        if (!self::checkPointInsideRectangle(array_merge($rect_arr,$output))){
            return false;
        }
        //y max
        $rotation_arr['rel_x'] = $input['width']-$input['rel_x'];
        $rotation_arr['rel_y'] = $input['height']-$input['rel_y'];
        $output = self::rotationPoint($rotation_arr);
        if (!self::checkPointInsideRectangle(array_merge($rect_arr,$output))){
            return false;
        }
        return true;
    }

    /**
     * struct{
     *    x_min
     *    x_max
     *    y_min
     *    y_max
     *    x
     *    y
     * }
     */
    static public function checkPointInsideRectangle($input){
        if ($input['x']>$input['x_max']){
            return false;
        }
        if ($input['x']<$input['x_min']){
            return false;
        }
        if ($input['y']>$input['y_max']){
            return false;
        }
        if ($input['y']<$input['y_min']){
            return false;
        }
        return true;
    }
    /**
     * 某个点绕另一个点逆时针旋转某个角度
     * input struct{
     *    base_x float
     *    base_y float
     *    rel_x float
     *    rel_y float
     *    hex   float
     * }
     * output struct{
     *    x float
     *    y float
     * }
     */
    static public  function rotationPoint($input){
        $rad = deg2rad(-$input['hex']);
        $output = array();
        $output['x'] = $input['rel_x']*cos($rad) -
            $input['rel_y']*sin($rad)+$input['base_x'];
        $output['y'] = $input['rel_x']*sin($rad) +
            $input['rel_y']*cos($rad)+$input['base_y'];
        return $output;
    }
}